﻿using System;
using System.Linq;
using System.Data.Linq;

namespace Test.DAL
{
    /// <summary>
    /// DalCMS_ARTICAL操作类
    /// </summary>
    public static class DalCMS_ARTICAL
    {
        /// <summary>
        /// 载入一个实体
        /// </summary>
        /// <param name="id">标识符列</param>
        /// <returns></returns>
        public static CMS_ARTICAL Load(this CMS_ARTICAL prd,Int64 id)
        {
            try
            {
                using (DataClassesTestDataContext objDataContext = new DataClassesTestDataContext())
                {
                    return (from objData in objDataContext.CMS_ARTICAL
                            where objData.ARTICAL_ID == id
                            select objData).SingleOrDefault();
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 保存一个实体
        /// </summary>
        /// <param name="localRecord">CMS_ARTICAL实体</param>
        /// <returns></returns>
        public static CMS_ARTICAL Save(this CMS_ARTICAL localRecord)
        {
            if (localRecord.ARTICAL_ID > 0)
            {
                return UpdateRecord(localRecord);
            }
            else
            {
                return InsertRecord(localRecord);
            }
        }

        /// <summary>
        /// 增加一个实体
        /// </summary>
        /// <param name="localRecord">CMS_ARTICAL实体</param>
        /// <returns></returns>
        private static CMS_ARTICAL InsertRecord(CMS_ARTICAL localRecord)
        {
            bool flag = false;
            try
            {
                using (DataClassesTestDataContext objDataContext = new DataClassesTestDataContext())
                {
                    objDataContext.CMS_ARTICAL.InsertOnSubmit(localRecord);
                    objDataContext.SubmitChanges();
                    flag = true;
                }
            }
            catch
            {
            }
            return flag ? localRecord : null;
        }

        /// <summary>
        /// 更新一个实体
        /// </summary>
        /// <param name="localRecord">CMS_ARTICAL实体</param>
        /// <returns></returns>
        private static CMS_ARTICAL UpdateRecord(CMS_ARTICAL localRecord)
        {
            bool flag = false;
            try
            {
                using (DataClassesTestDataContext objDataContext = new DataClassesTestDataContext())
                {
                    objDataContext.CMS_ARTICAL.Attach(localRecord);
                    objDataContext.Refresh(RefreshMode.KeepCurrentValues, localRecord);
                    objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
                    flag = true;
                }
            }
            catch(Exception ex)
            {
            }
            return flag ? localRecord : null;
        }

        /// <summary>
        /// 删除一个实体
        /// </summary>
        /// <param name="id">标识符列</param>
        /// <returns></returns>
        public static bool Delete(this CMS_ARTICAL prd,Int64 id)
        {
            bool flag = false;
            try
            {
                using (DataClassesTestDataContext objDataContext = new DataClassesTestDataContext())
                {
                    objDataContext.CMS_ARTICAL.DeleteOnSubmit((from objData in objDataContext.CMS_ARTICAL
                                                           where objData.ARTICAL_ID == id
                                                           select objData).SingleOrDefault());
                   objDataContext.SubmitChanges();
                   flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }
    }
}
